<?php

namespace app\api\controller;

use app\api\model\Holidays;
use app\api\model\house\HouseMedia;
use app\common\controller\Api;
use app\api\model\house\House as HouseModel;
use app\api\model\house\HouseOrder as HouseOrderModel;

class Housemanage extends Api
{

    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //业主房源登记
    public function apply()
    {
        try {
            $model = new HouseModel();
            $param = input();

            $param['user_id'] = $this->auth->id;
            $res = $model->apply($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);
    }

    //编号直达
    public function mylist()
    {
        $model = new HouseModel();
        $list = $model->mylist($this->auth->id);
        $this->success('',$list);

    }

    //我的房源列表
    public function lists()
    {
        $field = 'house_id,house_title,score,house_attr,house_level,mode,share_weekday_price,share_weekend_price,share_highday_price,
        is_group,comment_num,house_facilities_id,group_weekday_price,group_weekend_price,group_highday_price,describe_score,location_score,
        convenient_score,service_score,health_score,cost_performance_score';

        $list = HouseModel::where(['user_id'=>$this->auth->id])
            ->field($field)
            ->order('createtime desc')
            ->select();

        $holidays = Holidays::where(['type'=>0])->column('date');
        $workdays = Holidays::where(['type'=>1])->column('date');

        $date = date('Y-m-d');
        $day_of_week = date('N');

        foreach ($list as $k=>$v){
            $media = HouseMedia::where(['house_id'=>$v['house_id']])
                ->order('createtime asc')->column('media');
            $list[$k]['cover'] = $media[0];

            if (in_array($date, $holidays)) {
                $list[$k]['today_price'] = $v['share_highday_price'];
                $group_price_arr = $v['group_highday_price'];
                //continue;
            } else {
                if ($day_of_week == 6 || $day_of_week == 7) {
                    if (!in_array($date, $workdays)){
                        $list[$k]['today_price'] = $v['share_weekend_price'];
                        $group_price_arr = $v['group_weekend_price'];
                    } else {
                        $list[$k]['today_price'] = $v['share_weekday_price'];
                        $group_price_arr = $v['group_weekday_price'];
                    }
                    //continue;
                } else {
                    $list[$k]['today_price'] = $v['share_weekday_price'];
                    $group_price_arr = $v['group_weekday_price'];
                }
            }
            $price_arr = [$v['share_highday_price'],$v['share_weekend_price'],$v['share_weekday_price']];
            $list[$k]['mark_price'] = max($price_arr);
            $group_price_arr = json_decode($group_price_arr,true);
            foreach ($group_price_arr as $kk=>$vv){
                if ($vv['type']=='all'){
                    $group_price_arr[$kk]['title'] = '整房';
                } else {
                    $group_price_arr[$kk]['title'] = '卧室'.$vv['type'];
                }
            }
            $list[$k]['group_price'] = $group_price_arr;
        }

        $this->success('',$list);
    }

    //编辑房源的房源详情
    public function info()
    {
        try {
            $model = new HouseModel();
            $house_id = input('house_id',0);
            $info = $model->manageinfo($house_id,$this->auth->id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$info);
    }

    //编辑房源
    public function edit()
    {
        try {
            $model = new HouseModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->edit($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);

    }

    //删除房源
    public function del()
    {
        $house_id = input('house_id',0);
        $house_info = HouseModel::get(['user_id'=>$this->auth->id,'house_id'=>$house_id]);
        if (!$house_info){
            $this->error('不是该业主房源');
        }

        $order_count = HouseOrderModel::where(['house_id'=>$house_id])
            ->where('order_status','not in','0,-1')->count();

        if ($order_count>0){
            $this->error('存在进行中订单不能删除');
        }

        $res = $house_info->delete();
        if ($res){
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //计算房屋等级
    public function calculateHouseLevel()
    {

        $decorate_price = input('decorate_price',0);//装修费用
        $total_price = input('total_price',0);//总价
        $house_attr = input('house_attr',0);//房屋属性,0私人住宅(共享民居),1营运民宿(共享民宿)
        try {
            $model = new HouseModel();
            $res = $model->calculateHouseLevelTxt($decorate_price,$total_price,$house_attr);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);

    }

}